from flask import Blueprint,render_template,request,redirect,url_for,flash
from exts import db
from model.models import Dish,DishType
from fromVal.dishForm import DishAddForm,UpdateDishForm

bp = Blueprint("dish",__name__,url_prefix='/dish')


@bp.route("/")
def index():
    # 查询所有菜的信息
    dishes = Dish.query.all()
    dish_types= DishType.query.all()
    return render_template('dish/dish_index.html',dishes=dishes,dish_types=dish_types)


# 新增菜
@bp.route("/add",methods=['GET','POST'])
def add_dish():
    if request.method == 'GET':
        dish_types = DishType.query.all()
        return render_template("dish/add_dish.html",dish_types=dish_types)
    elif request.method == 'POST':
        form = DishAddForm(request.form)
        if form.validate():
            dish_name = form.dish_name.data
            price = form.price.data
            dish_type_no = form.dish_type_no.data
            dish = Dish(dish_name=dish_name,price=price,dish_type_no=dish_type_no)
            db.session.add(dish)
            db.session.commit()

            return redirect(url_for("dish.index"))
        else:
            for errors in form.errors.values():
                for error in errors:
                    flash(error)
            return redirect(url_for("dish.add_dish"))


# 依据菜品查询
@bp.route("/index_by_dish_type")
def index_by_dish_type():
    dish_type_no = int(request.args.get('dish_type_no'))
    dishes = Dish.query.filter(Dish.dish_type_no==dish_type_no).all()
    dish_types = DishType.query.all()
    return render_template('dish/dish_index.html',dishes=dishes,dish_types=dish_types)


@bp.route("/update_dish",methods=['GET','POST'])
def update_dish():
    dishes = []
    if request.method == 'GET':
        return render_template('dish/update_dish.html',dishes=dishes)
    elif request.method == 'POST':
        dish_name = request.form.get('dish_name')
        dishes = Dish.query.filter(Dish.dish_name.contains(dish_name))
        return render_template('dish/update_dish.html',dishes=dishes)


@bp.route("/show_update_dish/<dish_no>",methods=['POST','GET'])
def show_update_dish(dish_no):
    if request.method == 'GET':
        dish = Dish.query.filter(Dish.dish_no == int(dish_no)).first()
        dish_types = DishType.query.all()
        return render_template("dish/update_dish_detail.html",dish=dish,dish_types=dish_types)
    elif request.method == 'POST':
        form = UpdateDishForm(request.form)
        if form.validate():
            print(1)
            dish_name = form.dish_name.data
            price = form.price.data
            dish_type_no = form.dish_type_no.data
            dish = Dish.query.filter(Dish.dish_no==int(dish_no)).first()
            dish.dish_name = dish_name
            dish.price = price
            dish.dish_type_no =dish_type_no
            db.session.commit()
            print(dish_name)
            return redirect(url_for("dish.index"))

        else:
            for errors in form.errors.values():
                for error in errors:
                    flash(error)
            return redirect(url_for('dish.show_update_dish',dish_no=dish_no))